//
//  UserViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "UserViewController.h"
#import "SheZhiViewController.h"
#import "MyXiaZaiViewController.h"
#import "DengLuView.h"
#import "SanFangDengLuViewController.h"
#import "UIButton+WebCache.h"
#import "ExperienceDetailsViewController.h"
#import "HgTableViewCell.h"
#import "CacheTableViewCell.h"
#import "SearchResultTableViewCell.h"
#import "JCAlertView.h"
const CGFloat BackGroupHeight1 = 200;

@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, DengLuViewDelegate>

@property (nonatomic, retain) UIView *dengLuView;

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *imageBG;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) DengLuView *DLV;
@property (nonatomic, assign) BOOL isOutLunching;

@end

@implementation UserViewController

- (void)dealloc{
    [_dengLuView release];
    [_tableView release];
    [_imageBG release];
    [_titleLabel release];
    [_button release];
    [_DLV release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  
    self.view.backgroundColor = [UIColor yellowColor];
    
    
    
   NSArray *arr = [[DbHandel shareDbHandel] selectDengLuTable];
    
   
    if (arr.count > 0) {
        DengLuTabelModel *model = arr[0];
        self.navigationItem.title = model.userName;
       
    }
    

    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.contentInset = UIEdgeInsetsMake(BackGroupHeight1, 0, 0, 0);
    
    
    [_tableView registerClass:[SearchResultTableViewCell class] forCellReuseIdentifier:@"SearchResultTableViewCell"];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    
    [_tableView registerClass:[HgTableViewCell class] forCellReuseIdentifier:@"HgTableViewCell"];
    [_tableView registerClass:[CacheTableViewCell class] forCellReuseIdentifier:@"CacheTableViewCell"];
    
    
    /** 设置大的图片 */
    self.imageBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight1, self.view.frame.size.width, BackGroupHeight1)];
    
    //先取出轻量级缓存的图片
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"image"] == nil) {
        _imageBG.image = [UIImage imageNamed:@"d11898b3f116762539512213e98a52b0.jpg"];
        
    }else{
        NSData *imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
        UIImage *image = [UIImage imageWithData:imageData];
        _imageBG.image = image;
    }
    
    
    _imageBG.userInteractionEnabled = YES;
    [_tableView addSubview:_imageBG];
    [_imageBG release];
    
    /**<  添加一个手势 */
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    [_imageBG addGestureRecognizer:tap];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 150) / 2, -60, 150, 40)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [_tableView addSubview:label];
    [label release];
    
    

    
    UIImageView *imageView1 = [[UIImageView alloc ]initWithFrame:CGRectMake(200, -100, 80, 80)];
    imageView1.layer.cornerRadius = 40;
    
    imageView1.layer.masksToBounds = YES;
    imageView1.center = _imageBG.center;
    imageView1.backgroundColor = [UIColor clearColor];
    
    NSArray *arr1 = [[DbHandel shareDbHandel] selectDengLuTable];
    
    for (DengLuTabelModel *model  in arr1) {
        NSLog(@"%@", model.iconURL);
        [imageView1 sd_setImageWithURL:[NSURL URLWithString:model.iconURL] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
        label.text = [NSString stringWithFormat:@"%@", model.userName];
        
    }
    
    [_tableView addSubview:imageView1];
    
    
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(romoveButton:) name:@"denglucs" object:nil];

    
}
//登陆成功通知
- (void)romoveButton:(NSNotification *)notification{
    
    for (UIView *view in self.tabBarController.tabBar.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    
}



- (void)buttonAction:(UIButton *)button{
    NSLog(@"头像");
}


- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新个人封面" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        //访问相机
        UIImagePickerControllerSourceType sourseType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            //签订代理
            picker.delegate = self;
            
            //设置拍照后的图片可被编辑
            picker.allowsEditing = YES;
            picker.sourceType = sourseType;
            
            picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            
            [self presentViewController:picker animated:YES completion:^{
                
            }];
           
            [picker release];
            
        }else{
            NSLog(@"模拟机无法打开相机");
        }
        
        
        
        
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册上传" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //访问本地相册
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        //设置选折后的图片可以编辑
        picker.allowsEditing = YES;
        
        picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:picker animated:YES completion:^{
            
        }];
        [picker release];
        
        
     }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    [alert addAction:action1];
    [alert addAction:action2];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

/** 当选者一张图片后进入这里 */

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选折的类型是图片
    if ([type isEqualToString:@"public.image"]) {
        
        //先把图片转化成NSData
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        
        //关闭相册
        [picker dismissViewControllerAnimated:YES completion:^{
            
        }];
        
        [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"image"];
        
        _imageBG.image = image;
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"访问相册的取消按钮");
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultTableViewCell"];
    
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1];
        cell.userInteractionEnabled = NO;
        
        return cell;
    }
    
    if (indexPath.row == 3) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1];
        cell.userInteractionEnabled = NO;
        return cell;
    }
    
    if (indexPath.row == 6) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1];
        cell.userInteractionEnabled = NO;
        return cell;
    }
    if (indexPath.row == 1) {
        cell.lable1PlaveName.text = @"我的收藏";
        cell.imageViewHeader.image = [UIImage imageNamed:@"iconfont-wodeshoucang.png"];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    if (indexPath.row == 2) {
        
        HgTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"HgTableViewCell"];
        cell1.imageHeader.image = [UIImage imageNamed:@"iconfont-yejianmoshi.png"];
       cell1.label1.text = @"夜间模式";
       cell1.textLabel.textColor = [UIColor blackColor];
      cell1.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell1;
    }
    
    if (indexPath.row == 4) {
        cell.lable1PlaveName.text = @"我的下载";
        cell.imageViewHeader.image = [UIImage imageNamed:@"iconfont-download.png"];
                cell.textLabel.textColor = [UIColor blackColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    if (indexPath.row == 5) {
        cell.lable1PlaveName.text = @"关于我们";
        cell.imageViewHeader.image = [UIImage imageNamed:@"iconfont-fenxiang-2.png"];
               cell.textLabel.textColor = [UIColor blackColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    if (indexPath.row == 7) {
        CacheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CacheTableViewCell"];
        cell.imaeHeader.image = [UIImage imageNamed:@"iconfont-clear-2.png"];
        cell.label1.text = @"清除缓存";
        cell.textLabel.textColor = [UIColor blackColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    
    }
    
    if (indexPath.row == 8) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1];
        cell.userInteractionEnabled = NO;
        return cell;
    }
    
    if (indexPath.row == 9) {
        cell.lable1PlaveName.text = @"退出登录";
        cell.imageViewHeader.image = [UIImage imageNamed:@"iconfont-tuichudenglu.png"];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 10;
    }
    
    if (indexPath.row == 3) {
        return 10;
    }
    if (indexPath.row == 6) {
        return 10;
    }
    if (indexPath.row == 8) {
        return 10;
    }
    return 50;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 1) {
        NSArray *arr = [[DbHandel shareDbHandel] selectDailyTable];
        
        SheZhiViewController *shezhi = [[SheZhiViewController alloc] init];
        shezhi.arr = arr;
        
        [self.navigationController pushViewController:shezhi animated:YES];
        [shezhi release];
    }
    
    if (indexPath.row == 4) {
        
        MyXiaZaiViewController *my = [[MyXiaZaiViewController alloc] init];
        
        [self.navigationController pushViewController:my animated:YES];
        
        [my release];
        
    }
    
    if (indexPath.row == 5) {
        
        [JCAlertView showOneButtonWithTitle:@"关于我们" Message:@"蜜蜂旅行自立于为旅行爱好者提供优质的旅行攻略，旅行分享。官方邮箱m18242892440_1@163.com" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
        
        
    }
    
    
    if (indexPath.row == 7) {
        
        
        [self createAlertCaches];
        
    }

    
    
}
/**<  清理缓存alert */
- (void)createAlertCaches{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要清理缓存么" preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //确定就发送清理缓存通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"qinglihuanchun" object:nil];
        
    }];
    
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    
    [alert addAction:action];
    [alert addAction:action1];
    
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}




/** 实现协议方法 */

- (void)removeOtherButton{
    [_button removeFromSuperview];
}

//button点击方法
- (void)buttonAction1:(UIButton *)button{
    
    
    
    
    [UIView animateWithDuration:0.2 animations:^{
        _DLV.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
    }];
    
}

- (void)button111Action:(UIButton *)button{
    
    _isOutLunching = YES;
    UIWindow *WINDOW = [UIApplication sharedApplication].keyWindow;
    //创建登陆view
    self.DLV = [[DengLuView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    _DLV.delegate = self;
    [WINDOW addSubview:_DLV];
    _DLV.bolck(_isOutLunching);
    
    [_DLV release];
    
    
    [UIView animateWithDuration:0.2 animations:^{
        
        _DLV.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];

    
    
}

- (void)JumpToVC{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
